package 数组;

public class L59 {

    public static void main(String[] args) {

    }

    public int[][] generateMatrix(int n) {

        int[][] generate = new int[n][n];

        int k = 1, top = 0, right = n - 1, bottom = n - 1, left = 0;
        while (k < n*n) {
            for (int i = left; i <= right; i++, k++) generate[top][i] = k;
            top++;
            for (int i = top; i <= bottom; i++, k++) generate[i][right] = k;
            right--;
            for (int i = right; i >= left; i--, k++) generate[bottom][i] = k;
            bottom--;
            for (int i = bottom; i >= top; i--, k++) generate[i][left] = k;
            left++;
        }
        return generate;
    }
}
